package com.itheima.health.mobile.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Res;
import com.itheima.health.service.ResCheckoutService;
import com.itheima.health.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 验证码Controller
 */
@RestController
@RequestMapping("/mobile/validateCode")
@Slf4j
public class ValidateCodeController {

    @Reference
    private SmsService smsService;

    @Reference
    ResCheckoutService resCheckoutService;

    /**
     * 发送验证码
     *
     * @param type      验证码类型，用于区分不同的业务
     * @param telephone 手机号
     * @return
     */
    @RequestMapping("/send")
    public Result send(String type, String telephone) {
        //发送验证码之前先查询redis，是否通过图形验证码校验


        log.info("[验证码-发送]type:{},telephone:{}", type, telephone);
    public Result send(String type, String telephone , @RequestBody Res res, HttpServletRequest request) {
        log.info("[验证码-发送]type:{},telephone:{},res:{}", type, telephone, res);

        String userIp = request.getHeader("X-Real-IP");

        boolean b = resCheckoutService.resCheckout(res, userIp);

        if (!b){
            return new Result(false, "请勿重复发送验证码");
        }

        //rpc调用发送验证码
        smsService.sendValidateCode(type, telephone);
        return new Result(true, MessageConst.SEND_VALIDATECODE_SUCCESS);
    }
}
